package org.video.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import org.video.mine.GlobalStr;
import org.video.mine.R;
import org.video.mine.databinding.RecyDownItemBinding;
import org.video.mine.entity.MoviePlayerEntity;

import java.util.List;

public class RecyDownAdapter extends RecyclerView.Adapter<GlobalStr.ViewHolder> {

    List<MoviePlayerEntity> playerEntities;
    Context context;
    private OnItemClickListener click;

    public RecyDownAdapter(List<MoviePlayerEntity> playerEntities, Context context) {
        this.playerEntities = playerEntities;
        this.context = context;
    }

    @NonNull
    @Override
    public GlobalStr.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyDownItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recy_down_item,parent,false);

        return new GlobalStr.ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull GlobalStr.ViewHolder holder, final int position) {
        MoviePlayerEntity moviePlayerEntity = playerEntities.get(position);
        final RecyDownItemBinding binding = (RecyDownItemBinding) holder.getTitleBinding();
        binding.tvName.setText(moviePlayerEntity.getName());
        binding.llDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.cbChoose.setChecked(!binding.cbChoose.isChecked());
                if (click!=null){
                    click.onItemClick(binding.llDown,position);
                }
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull GlobalStr.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()){
            super.onBindViewHolder(holder,position,payloads);
            return;
        }
        RecyDownItemBinding binding = (RecyDownItemBinding) holder.getTitleBinding();
        MoviePlayerEntity moviePlayerEntity = playerEntities.get(position);
        for (Object payload:payloads) {
            switch (String.valueOf(payload)){
                case "check":
                    binding.cbChoose.setChecked(moviePlayerEntity.isCheck());
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return playerEntities.size();
    }

    public void setOnItemClick(OnItemClickListener click){
        this.click = click;
    }

    public interface OnItemClickListener{
        void onItemClick(View v, int position);
    }
}
